package light.head.dao.mobile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.ApplyInfo;

import org.springframework.stereotype.Service;

/**
 * 物品申请
 * @author yangguanghe @date 2014-5-21 @time 下午11:28:26
 *
 */
@Service
public class MobileGoodsApplyDAO extends BaseDAO<ApplyInfo, Integer> {
	
	
	/**
	 * 查询方法
	 *@param cu 	创建人
	 *@param map	查询条件
	 *@param pb		分页信息
	 *@author yangguanghe [2014-5-21_下午11:41:36]
	 */
	public RESULT findByMap (Integer cu, Map<String, String> map, PageBean pb) {
		if (null == cu) return null;
		StringBuffer hql = new StringBuffer("FROM ApplyInfo a WHERE a.au = ? AND a.deleted = 0");
		
		List<Object> parm = new ArrayList<Object>();
		parm.add(cu);
		if (null != map) {
			
			String title = map.get(Parm.TITLE);
			String et = map.get(Parm.ET);
			String st = map.get(Parm.ST);
			
			if (isNotNull(title)) {
				hql.append(" AND a.title LIKE ?");
				parm.add("%" + title + "%");
			}
			
			if (isNotNull(st) && isNotNull(et)) {
				hql.append(" AND a.at BETWEEN ? AND ?");
				parm.add(DateUtils.getDateByStr(st, "yyyy-MM-dd"));
				parm.add(DateUtils.getDateByStr(et, "yyyy-MM-dd"));
			} else if (isNotNull(st)  && !isNotNull(et)) {
				hql.append(" AND a.at >= ?");
				parm.add(DateUtils.getDateByStr(st, "yyyy-MM-dd"));
			} else if (!isNotNull(st)  && isNotNull(et)) {
				hql.append(" AND a.at <= ?");
				parm.add(DateUtils.getDateByStr(et, "yyyy-MM-dd"));
			}
			
		}
		
		hql.append(" ORDER BY a.code DESC");
		
		return findToPageBean(hql.toString(), pb, parm);
	}

	
	public RESULT update(ApplyInfo info) {
		
		if (null == info || null == info.getCode()) return RESULT.PARAMETER_ERROR;
		
		ApplyInfo db = super.get(info.getCode());
		
		if (null == db) {
			return RESULT.NOT_FIND;
		}
		
		boolean ok = false;
		
		if (null == db.getTitle() || !db.getTitle().equals(info.getTitle())) {
			ok = true;
			db.setTitle(info.getTitle());
		}
		
		if (null == db.getSort() || (null != info.getSort() && db.getSort().byteValue() != info.getSort().byteValue())) {
			ok = true;
			db.setSort(info.getSort());
		}
		
		if (ok) {
			return super.upd(db);
		}
		
		return RESULT.SUCCESS;
	}
		
	public RESULT findByMap_audit (List<Integer> ocs, Integer rc, Map<String, String> map , PageBean pb) {
		if (null == ocs || null == rc) return null;
		
		List<Object> parm = new ArrayList<Object>();	
		List<Object> countParm = new ArrayList<Object>();
		
		StringBuffer temp = new StringBuffer();
		temp.append(" FROM ApplyInfo a");
		temp.append(" JOIN UserInfo u ON u.code = a.au");
		temp.append(" JOIN Organ o ON o.code = a.oc");
		temp.append(" JOIN AuditStatus s on s.mc = a.code and s.beforeStatus = 1 WHERE a.deleted = 0 AND s.business = 1 AND s.rc = ? and a.oc IN (");
		
		parm.add(rc);
		countParm.add(rc);
		for (Integer ig : ocs) {
			temp.append("?,");
			parm.add(ig);
			countParm.add(ig);
		}
		
		temp.deleteCharAt(temp.length() - 1).append(")");
		
		if(map != null){
			String deleted = map.get(Parm.DELETED);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String title = map.get(Parm.TITLE);
			
			if (isNotNull(deleted)) {
				parm.add(Boolean.parseBoolean(deleted));
				countParm.add(Boolean.parseBoolean(deleted));
				temp.append(" AND a.deleted = ?");
			}
			
			if (isNotNull(time1)) {
				parm.add(DateUtils.getDateByStr(time1, null));
				countParm.add(DateUtils.getDateByStr(time1, null));
				temp.append(" AND a.at >= ?");
			}
			
			if(isNotNull(time2)){
				parm.add(DateUtils.getDateByStr(time2, null));
				countParm.add(DateUtils.getDateByStr(time2, null));
				temp.append(" AND a.at <= ?");
			}
			
			if(isNotNull(title)) {
				parm.add("%" + title + "%");
				countParm.add("%" + title + "%");
				temp.append(" AND a.title LIKE ?");
			}
		}	 
		
		StringBuffer sql = new StringBuffer("SELECT a.code, a.title, a.au, a.at, a.oc, a.deleted, u.realName, o.name");
		StringBuffer countSql = new StringBuffer("SELECT COUNT(*)");
		
		sql.append(temp).append(" ORDER BY a.code DESC");
		countSql.append(temp);
		
		if (RESULT.SUCCESS == queryToPageBean(pb, sql.toString(), parm, countSql.toString(), countParm))
			return packageResult(pb);
		else
			return RESULT.ERROR;
	}
	/***
	 * 封装
	 * @param pb
	 * @return
	 */
	private RESULT packageResult(PageBean pb) {
		
		ApplyInfo info = null;
		
		List<ApplyInfo> list = new ArrayList<ApplyInfo>();
		
		//  a.code, a.title, a.au, a.at, a.oc, a.deleted, u.realName, o.name
		for (Object o : pb.getResultList()) {
			
			info = new ApplyInfo();
			Object[] obj = (Object[]) o;
			
			Integer code = (null == obj[0] || Sys.NONE.equals(obj[0].toString())) ? null : (Integer) obj[0];
			String title = (null == obj[1] || Sys.NONE.equals(obj[1].toString())) ? null : (String) obj[1];
			Integer au = (null == obj[2] || Sys.NONE.equals(obj[2].toString())) ? null : (Integer) obj[2];
			Date at = (null == obj[3] || Sys.NONE.equals(obj[3].toString())) ? null : (Date) obj[3];
			Integer oc = (null == obj[4] || Sys.NONE.equals(obj[4].toString())) ? null : (Integer) obj[4];
			Boolean deleted = (null == obj[5] || Sys.NONE.equals(obj[5].toString())) ? null : (Boolean) obj[5];
			String an = (null == obj[6] || Sys.NONE.equals(obj[6].toString())) ? null : (String) obj[6];
			String ocn = (null == obj[7] || Sys.NONE.equals(obj[7].toString())) ? null : (String) obj[7];
			
			info.setCode(code);
			info.setTitle(title);
			info.setAu(au);
			info.setAt(at);
			info.setOc(oc);
			info.setDeleted(deleted);
			info.setAn(an);
			info.setOcn(ocn);
			
			list.add(info);
		}
		
		pb.setResultList(list);
		
		return RESULT.SUCCESS;
	}
}
